<html>
	<head>
		<script src="../gears_init.js"></script>
		<script src="../GearsORM.js"></script>
		<script src="../GearsORM.Events.js"></script>
		<script src="../GearsORM.ResultIterator.js"></script>
		<script src="../GearsORM.Sql.js"></script>
		<script src="../GearsORM.Fields.js"></script>
		<script src="../GearsORM.Model.js"></script>
		<script src="../GearsORM.Introspection.js"></script>
		<script src="../GearsORM.Transaction.js"></script>
		<script>
			GearsORM.dbName = "test.relations";
			var Corporation = new GearsORM.Model({
				name:"Corporation",
				fields:
				{
					name:new GearsORM.Fields.String({maxLength:25}),
					companies:new GearsORM.Fields.ManyToOne({related:"Company"})
				}	
			});
			
			var Company = new GearsORM.Model({
				name:"Company",
				fields:
				{
					name:new GearsORM.Fields.String({maxLength:25}),
					pepole:new GearsORM.Fields.ManyToOne({related:"Person"}),
					corporation:new GearsORM.Fields.OneToMany({related:"Corporation"})
				}
			});
			
			var Person = new GearsORM.Model({
				name:"Person",
				fields:
				{
					firstName:new GearsORM.Fields.String({maxLength:25}),
					lastName:new GearsORM.Fields.String({maxLength:25}),
					company:new GearsORM.Fields.OneToMany({related:"Company",onDeleteCascade:true})	
				}
			});
			
			var Tests =
			{
				up:function()
				{
					Company.dropTable();
					Person.dropTable();
					Corporation.dropTable();
					Corporation.createTable();
					Company.createTable();
					Person.createTable();
					var corp = (new Corporation({name:"Corporation1"})).save();
					(new Company({name:"Company1",corporation:corp})).save();
					(new Company({name:"Company2",corporation:corp})).save();
				},
				"test if create works with relation":function()
				{
					var someone = new Person();
					someone.firstName = "Jhon";
					someone.lastName = "Doe";
					someone.company = Company.select("Company.name = ?",["Company1"]).getOne();
					someone.save();
					UnitTest.assertDefined(someone.rowid);
					var someone2 = new Person();
					someone2.firstName = "Jhon";
					someone2.lastName = "Doe";
					someone2.company = Company.select("Company.name = ?",["Company2"]).getOne();
					someone2.save();
					UnitTest.assertDefined(someone2.rowid);
				},
				"test select from backward relation":function()
				{
					var company1 = Company.select("Company.name = ?",["Company1"]).getOne();
					UnitTest.assertTrue(
						company1.pepole.select().toArray().length == 1,
						"there should be only one person in company1"
					);	
				},
				"test remove from backward relation":function()
				{
					var company2 = Company.select("Company.name = ?",["Company2"]).getOne();
					var someone2 = new Person(2);
					someone2.remove();
					UnitTest.assertTrue(
						company2.pepole.select().getOne() == false,
						"someone from company2 wasn`t deleted"
					);
				},
				"test cascade on delete":function()
				{
					var aCompany = new Company();
					aCompany.name = "aCompany";
					aCompany.corporation = Corporation.select().getOne();
					aCompany.save();
					var aPerson = new Person();
					aPerson.firstName = "John";
					aPerson.lastName = "Doe";
					aPerson.company = aCompany;
					aPerson.save();
					aCompany.remove();
					UnitTest.assertTrue(
						aCompany.pepole.select().getOne() == false,
						"person wasn`t deleted"
					);
					aCompany.remove();
				},
				"test for 2+ level depth of relations":function()
				{
					var corp = new Corporation();
					corp.name = "Test Corporation 1";
					corp.save();
					var comp = new Company();
					comp.name = "Test Company 1";
					comp.corporation = corp;
					comp.save();
					var person = new Person();
					person.firstName = "John";
					person.lastName = "Doe";
					person.company = comp;
					person.save();
					
					var personFromDb = new Person(person.rowid);
					UnitTest.assertTrue(
						!!personFromDb.company &&
						!!personFromDb.company.corporation &&
						personFromDb.company.rowid == comp.rowid &&
						personFromDb.company.corporation.rowid == corp.rowid,
						"related models weren`t selected"
					);
				},
				"test if forigen key is enforced in a insert":function()
				{
					var someone = new Person();
					someone.firstName = "John";
					someone.lastName = "Doe";
					someone.company = 100;//a invalid company value
					try
					{
						someone.save();
					}
					catch(e)
					{
						return;
					}
					throw new Error("a exception didn`t happen even that the insert vaiolate the forigen key");
				},
				"test if forigen key is enforced in a update":function()
				{
					var someone = new Person();
					someone.firstName = "John";
					someone.lastName = "Doe";
					someone.company = 1;
					someone.save();
					try
					{
						someone.company = 100;//a invalid company value
						someone.save();
					}
					catch(e)
					{
						return;
					}
					throw new Error("a exception didn`t happen even that the insert vaiolate the forigen key");
				},
				down:function()
				{
					Person.dropTable();
					Company.dropTable();
				}
			};
		</script>
		<script src="unit_test.js"></script>
	</head>
	<body>
		
	</body>
</html>